package com.siao.component
{
	import com.siao.display.LayoutElement;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormatAlign;
	
	/**
	 * 补全缩略文本
	 * @author SiaoLeon
	 */
	public class ExtendTextField extends ThumbTextField
	{
		// -------------------------------------------------------------------------------------------------------- 静态常量
		
		// -------------------------------------------------------------------------------------------------------- 静态变量
		
		// -------------------------------------------------------------------------------------------------------- 静态方法
		
		// -------------------------------------------------------------------------------------------------------- 静态函数
		
		// -------------------------------------------------------------------------------------------------------- 属性
		
		override public function set selectable(value:Boolean):void
		{
			super.selectable = false;
		}
		
		override public function get selectable():Boolean
		{
			return false;
		}
		
		override public function set text(value:String):void
		{
			super.text = value;
			
			this.onExtendRollOut();
		}
		
		// -------------------------------------------------------------------------------------------------------- 变量
		
		private var extend:LayoutElement;
		
		private var fulltext:TextField;
		
		private var isOver:Boolean;
		
		// -------------------------------------------------------------------------------------------------------- 构造
		
		/**
		 * 构造函数
		 */
		public function ExtendTextField(ellipsis:String="...", color:uint=0XFFFFFF, alpha:Number=0, buttonMode:Boolean=false)
		{
			super(ellipsis);
			
			this.extend = new LayoutElement();
			this.extend.mouseChildren = false;
			this.extend.buttonMode = buttonMode;
			this.extend.draw(color, alpha);
			
			this.fulltext = new TextField();
			this.fulltext.x = 2;
			this.fulltext.y = 1;
			this.extend.addChild(this.fulltext);
			
			this.selectable = false;
			
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage, false, 0, true);
		}
		
		// -------------------------------------------------------------------------------------------------------- 方法
		
		/**
		 * 绘制背景
		 * @param color
		 * @param alpha
		 */
		public function draw(color:uint=0X000000, alpha:Number=1.0):void
		{
			this.extend.draw(color, alpha);
		}
		
		// -------------------------------------------------------------------------------------------------------- 函数
		
		protected function onAddedToStage(event:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage, false, 0, true);
			
			this.addEventListener(MouseEvent.ROLL_OVER, onRollOver, false, 0, true);
		}
		
		protected function onRemovedFromStage(event:Event):void
		{
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage, false, 0, true);
			this.removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
			
			this.extend.removeEventListener(MouseEvent.ROLL_OUT, onExtendRollOut);
			
			if (this.stage != null)
			{
				this.stage.removeEventListener(Event.MOUSE_LEAVE, onExtendRollOut);
			}
			
			this.extend.remove();
		}
		
		/**
		 * 鼠标移入时
		 * @param event
		 */
		private function onRollOver(event:MouseEvent):void
		{
			if (!this.isThumb)
			{
				return;
			}
			
			this.extend.addEventListener(MouseEvent.ROLL_OVER, onExtendRollOver, false, 0, true);
			this.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove, false, 0, true);
			
			this.visible = false;
			
			this.fulltext.selectable = false;
			this.fulltext.defaultTextFormat = this.defaultTextFormat;
			this.fulltext.styleSheet = this.styleSheet;
			this.fulltext.antiAliasType = this.antiAliasType;
			this.fulltext.embedFonts = this.embedFonts;
			this.fulltext.filters = this.filters;
			this.fulltext.gridFitType = this.gridFitType;
			this.fulltext.sharpness = this.sharpness;
			this.fulltext.thickness = this.thickness;
			this.fulltext.maxChars = this.maxChars;
			this.fulltext.restrict = this.restrict;
			this.fulltext.multiline = this.multiline;
			this.fulltext.wordWrap = this.wordWrap;
			this.fulltext.autoSize = TextFieldAutoSize.LEFT;
			this.fulltext.text = (this.text == null) ? "" : this.text;
			
			if (this.multiline)
			{
				this.fulltext.width = this.width;
			}
			else
			{
				this.fulltext.height = this.height;
			}
			
			this.extend.width = this.fulltext.width + 4;
			this.extend.height = this.fulltext.height + 2;
			
			this.extend.x = (this.defaultTextFormat.align == TextFormatAlign.CENTER) ? this.localToGlobal(new Point((this.width - this.extend.width) / 2, 0)).x - 1 : this.localToGlobal(new Point(0, 0)).x - 2;
			this.extend.y = this.localToGlobal(new Point(0, 0)).y - 1;
			
			if (this.extend.x + this.extend.width > this.stage.stageWidth)
			{
				this.extend.x = this.stage.stageWidth - this.extend.width;
			}
			
			if (this.extend.y + this.extend.height > this.stage.stageHeight)
			{
				this.extend.y = this.stage.stageHeight - this.extend.height;
			}
			
			this.stage.addChild(this.extend);
		}
		
		/**
		 * 鼠标移动时
		 * @param event
		 */
		private function onMouseMove(event:MouseEvent):void
		{
			if (!this.isOver)
			{
				this.onExtendRollOut();
			}
		}
		
		/**
		 * 扩展鼠标点击时
		 * @param event
		 */
		private function onExtendClick(event:MouseEvent):void
		{
			this.dispatchEvent(event);
		}
		
		/**
		 * 扩展鼠标移入时
		 * @param event
		 */
		private function onExtendRollOver(event:MouseEvent):void
		{
			this.extend.addEventListener(MouseEvent.CLICK, onExtendClick, false, 0, true);
			this.extend.addEventListener(MouseEvent.ROLL_OUT, onExtendRollOut, false, 0, true);
			this.stage.addEventListener(Event.MOUSE_LEAVE, onExtendRollOut, false, 0, true);
			
			this.isOver = true;
		}
		
		/**
		 * 扩展鼠标移出时
		 * @param event
		 */
		private function onExtendRollOut(event:Event=null):void
		{
			this.isOver = false;
			this.visible = true;
			
			this.extend.removeEventListener(MouseEvent.CLICK, onExtendClick);
			this.extend.removeEventListener(MouseEvent.ROLL_OVER, onExtendRollOver);
			this.extend.removeEventListener(MouseEvent.ROLL_OUT, onExtendRollOut);
			
			if (this.stage != null)
			{
				this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
				this.stage.removeEventListener(Event.MOUSE_LEAVE, onExtendRollOut);
			}
			
			this.extend.remove();
		}
	
	}
}
